package com.example.demo.web.common.filter;

import com.jfinal.kit.Kv;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.text.StrUtil;
import org.noear.snack.ONode;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Filter;
import org.noear.solon.core.handle.FilterChain;

/**
 * @author airhead
 */
@Slf4j
@Component
public class GlobalFilter implements Filter {
  @Override
  public void doFilter(Context ctx, FilterChain chain) throws Throwable {
    String path = ctx.path();
    log.info("1. GlobalFilter, path: {}", path);
    // 简单逻辑，非静态的路径都需要token
    if (!path.contains(".")) {
      String token = ctx.param("token");
      if (StrUtil.isBlank(token)) {
        ctx.outputAsJson(ONode.stringify(Kv.of("state", false)));
        return;
      }
    }

    chain.doFilter(ctx);
  }
}
